export default {
    namespaced: true,
    state: {
        courseSteps: {} // 使用对象存储不同课程的步骤 { courseId: steps[] }
    },
    getters: {
        getCourseSteps: (state) => (courseId) => state.courseSteps[courseId]
    },
    mutations: {
        SET_STEPS(state, { courseId, steps }) {
            state.courseSteps[courseId] = steps;
        },
        TOGGLE_STEP(state, { courseId, index }) {
            if (state.courseSteps[courseId]) {
                state.courseSteps[courseId][index].completed = !state.courseSteps[courseId][index].completed;
            }
        }
    },
    actions: {
        async fetchSteps({ commit }, courseId) {
            try {
                // 从本地存储获取数据
                const savedSteps = localStorage.getItem(`course_steps_${courseId}`);
                if (savedSteps) {
                    commit('SET_STEPS', { courseId, steps: JSON.parse(savedSteps) });
                }
            } catch (error) {
                console.error('Error fetching steps:', error);
            }
        },
        async saveSteps({ commit }, { courseId, steps }) {
            try {
                // 保存到本地存储
                localStorage.setItem(`course_steps_${courseId}`, JSON.stringify(steps));
                commit('SET_STEPS', { courseId, steps });
            } catch (error) {
                console.error('Error saving steps:', error);
            }
        },
        toggleStep({ commit, state }, { courseId, index }) {
            commit('TOGGLE_STEP', { courseId, index });
            // 保存更改到本地存储
            const steps = state.courseSteps[courseId];
            if (steps) {
                localStorage.setItem(`course_steps_${courseId}`, JSON.stringify(steps));
            }
        }
    }
};